package com.traffic.managent.serializable;

import java.util.HashMap;
import java.util.Map;

public class SerializerManage {

	private static final Map<String, ISerializer3> map = new HashMap<String, ISerializer3>();
	private static String DEFAULT_KEY = null;

	static {
		JRESerializer3 jRESerializer3 = new JRESerializer3();
		add(jRESerializer3);
		DEFAULT_KEY = jRESerializer3.name();
	}

	public static void setDefaultKey(String defaultKey) {
		DEFAULT_KEY = defaultKey;
	}

	/**
	 * V3.1以前版本兼容
	 * @param key
	 * @param iSerializer
	 */
	public static void add(String key, ISerializer3 iSerializer) {
		map.put(key, iSerializer);
	}

	public static void add(ISerializer3 iSerializer) {
		map.put(iSerializer.name(), iSerializer);
	}

	public static ISerializer3 get(String key) {
		return map.get(key);
	}

	public static ISerializer3 getDefault() {
		return map.get(DEFAULT_KEY);
	}

	public static byte[] serialize(Object obj) throws Exception {
		return getDefault().serialize(obj);
	}

	public static Object deserialize(byte[] bytes) throws Exception {
		return getDefault().deserialize(bytes);
	}
}
